package com.example.bestapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.bestapp.R;
import com.example.bestapp.RecycleViewDemo;
import com.example.bestapp.bean.UserBean;
import com.example.bestapp.utils.LogUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class RecycleViewDemoAdapter extends RecyclerView.Adapter<RecycleViewDemoAdapter.MyViewHolde> {
    private Context context;
    private ArrayList<UserBean> datalist;
    private boolean isSelect=false;

    private Set<Integer> positionList=new HashSet<>();
    public RecycleViewDemoAdapter(Context context, ArrayList<UserBean> datalist) {
        this.context = context;
        this.datalist = datalist;
    }

    @NonNull
    @Override
    public MyViewHolde onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       final View itemview= LayoutInflater.from(context).inflate(R.layout.recycleview_item,parent,false);
        return new MyViewHolde(itemview);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolde holder, int position) {
        UserBean userBean=datalist.get(position);
        holder.textView.setText(userBean.getName());
        Glide.with(context)
                .load(userBean.getImg())
                .fitCenter()
                .error(R.drawable.ic_launcher_background)
                .into(holder.imageView);
//        ---------------------------------------------------------------------------------
        if (UserBean.isShow){
            holder.iv_select.setVisibility(View.VISIBLE);
            if (userBean.getSelect()){
                holder.iv_select.setImageResource(R.drawable.select__1_);
            }else {
                holder.iv_select.setImageResource(R.drawable.select);
            }
        }
//         ----------------------------------------------------------------------------------
    }
    public  Set<Integer> getPosition(){
       return positionList;
    }
    @Override
    public int getItemCount() {
        return datalist.size();
    }

    public class MyViewHolde extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;
        ImageView iv_select;
        public MyViewHolde(@NonNull View itemView) {
            super(itemView);
            iv_select = itemView.findViewById(R.id.iv_select);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if(!RecycleViewDemo.getIsSelect()) {
//                        isSelect = !isSelect;
//                        if (isSelect) {
//                            iv_select.setImageResource(R.drawable.select__1_);
//                            positionList.add(getAdapterPosition());
//                        } else {
//                            iv_select.setImageResource(R.drawable.select);
//                        }
//                    }
                    if (!UserBean.isShow)return;
                    final int currentPosition = getAdapterPosition();
                    final UserBean currentPerson = datalist.get(currentPosition);
                    currentPerson.setSelect(!currentPerson.getSelect());
                    LogUtil.d("www",currentPerson.toString());
                    notifyItemChanged(currentPosition);
                }
            });
        }
    }

}
